import requests

import app
from api.login_api import LoginAPI


class Department(object):
    def __init__(self):
        # 组织架构url
        # 获取部门url
        # 添加部门url
        # 修改部门url
        # 删除部门url
        self.modify_depart_url = self.add_depart_url = self.get_depart_url = self.structure_url = self.del_depart_url = \
            app.BASE_URL + "/api/company/department"
        # 获取token
        # self.token = "Bearer " + LoginAPI().login("13800000002", "123456").json().get("data")
        # # token放入全局请求头
        # app.HEADERS["Authorization"] = self.token

    # 获取组织架构信息
    def get_department_structure(self, headers):
        response = requests.get(url=self.structure_url, headers=headers)
        return response

    # 获取部门信息
    def get_department_info(self, id, headers):
        response = requests.get(url=self.structure_url + "/" + id, headers=headers)
        return response

    # 添加部门信息
    def add_department(self, jsonData, headers):
        response = requests.post(url=self.structure_url, headers=headers, json=jsonData)
        return response

    # 修改部门信息
    def modify_department(self, id, jsonData, headers):
        response = requests.put(url=self.structure_url + "/" + id, headers=headers, json=jsonData)
        return response

    # 删除部门
    def del_department(self, id, headers):
        response = requests.delete(url=self.structure_url + "/" + id, headers=headers)
        return response


if __name__ == '__main__':
    dept = Department()
    login = LoginAPI()
    token = "Bearer " + login.login("13800000002", "123456").json().get("data")
    headers = {"Authorization": token}
    print("获取组织架构:", dept.get_department_structure(headers).json())
    re = dept.add_department({"name": "东西", "code": "911", "manager": "懂", "introduce": "测试"}, headers).json()
    print("添加部门:", re)
    app.DEPT_ID = re.get("data").get("id")
    print("打印部门ID:", app.DEPT_ID)
    print("获取部门信息:", dept.get_department_info(app.DEPT_ID, headers).json())
    print("修改部门信息:", dept.modify_department(app.DEPT_ID, {"name": "南北"}, headers).json())
    print("删除部门:", dept.del_department(app.DEPT_ID, headers).json())
